package com.exartisan.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.exartisan.bean.Event;
import com.exartisan.bean.Gallery;
import com.exartisan.bean.GalleryContent;

public class StupidResponseTranslater {
	
	public static boolean checkSuccess(String jsonResponse){
		JSONObject jsonObject;
		boolean isSuccess = false;
		try{
		jsonObject = new JSONObject(jsonResponse);
		if(jsonObject.getString(NetworkUtility.HANDLE).equals(NetworkUtility.ON_SYSTEM_ERROR)) isSuccess = false;
		else isSuccess = true;
		}
		catch (JSONException e) {
			// TODO: handle exception
		}
		return isSuccess;
	}
	
	public static String getHandle(String jsonResponse){
		JSONObject jsonObject;
		String handle = "";
		try {
			jsonObject = new JSONObject(jsonResponse);
			handle = jsonObject.getString(NetworkUtility.HANDLE);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return handle;
	}
	
	public static String getErrorMessage(String jsonResponse){
		JSONObject jsonObject;
		String msg = "";
		try{
		jsonObject = new JSONObject(jsonResponse);
		msg = jsonObject.getString(NetworkUtility.ERROR_MESSAGE);
		} catch(JSONException e){
			
		}
		return msg;
	}
	
	public static ArrayList<Event> getTopEvent(String jsonResponse){
		ArrayList<Event> alEvents = new ArrayList<Event>();
		long eventID;
		int type; //0: top event | 1: new event
		String eventName;
		String startDate;
		String endDate;
		String thumbUrl;
		String fbFeedID = null;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonQuestion = jsonRp.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				eventID = jsonElement.getLong(NetworkUtility.ID);
				type = 0;
				eventName = jsonElement.getString(NetworkUtility.EVENTNAME);
				startDate = jsonElement.getString(NetworkUtility.STARTDATE);
				endDate = jsonElement.getString(NetworkUtility.ENDDATE);
				thumbUrl = jsonElement.getString(NetworkUtility.THUMBURL);
				Event event = new Event(eventID, type, eventName, startDate, endDate, thumbUrl, fbFeedID);
				event.setArticle(jsonElement.getString(NetworkUtility.ARTICLE));
				event.setDescription(jsonElement.getString(NetworkUtility.DESCRIPTION));
				event.setCoverUrl(jsonElement.getString(NetworkUtility.THUMBURL));
				event.setFbFeedID(jsonElement.getString(NetworkUtility.FEEDID));
				alEvents.add(event);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return alEvents;
	}
	
	public static ArrayList<Event> getNewEvent(String jsonResponse){
		ArrayList<Event> alEvents = new ArrayList<Event>();
		long eventID;
		int type; //0: top event | 1: new event
		String eventName;
		String startDate;
		String endDate;
		String thumbUrl;
		String description;
		String fbFeedID = null;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonQuestion = jsonRp.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				eventID = jsonElement.getLong(NetworkUtility.ID);
				type = 0;
				eventName = jsonElement.getString(NetworkUtility.EVENTNAME);
				startDate = jsonElement.getString(NetworkUtility.STARTDATE);
				endDate = jsonElement.getString(NetworkUtility.ENDDATE);
				thumbUrl = jsonElement.getString(NetworkUtility.THUMBURL);
				description = jsonElement.getString(NetworkUtility.DESCRIPTION);
				Event event = new Event(eventID, type, eventName, startDate, endDate, thumbUrl,description,fbFeedID);
				event.setArticle(jsonElement.getString(NetworkUtility.ARTICLE));
				event.setCoverUrl(jsonElement.getString(NetworkUtility.THUMBURL));
				event.setFbFeedID(jsonElement.getString(NetworkUtility.FEEDID));
				alEvents.add(event);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return alEvents;
	}
	
	public static ArrayList<Gallery> getGalleryInfo(String jsonResponse){
		ArrayList<Gallery> galleries = new ArrayList<Gallery>();
		long galleryID;
		long eventID;
		String eventName;
		String thumbURL;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonQuestion = jsonRp.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				galleryID = jsonElement.getLong(NetworkUtility.ID);
				eventID = jsonElement.getLong(NetworkUtility.EVENTID);
				eventName = jsonElement.getString(NetworkUtility.EVENTNAME2);
				thumbURL = jsonElement.getString(NetworkUtility.MEDIA_URL);
				Gallery gallery = new Gallery(galleryID, eventID, eventName, thumbURL);
				galleries.add(gallery);
			}		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return galleries;
	}
	
	public static ArrayList<GalleryContent> getEventGallery(String jsonResponse){
		ArrayList<GalleryContent> contents = new ArrayList<GalleryContent>();
		long contentID;
		String resourceUrl;
		String description;
		int type;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonQuestion = jsonRp.getJSONObject(NetworkUtility.QUESTION);
			JSONArray jsonTable = jsonQuestion.getJSONArray(NetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				contentID = jsonElement.getLong(NetworkUtility.ID);
				resourceUrl = jsonElement.getString(NetworkUtility.MEDIA_URL);
				type = jsonElement.getInt(NetworkUtility.TYPE);
				description = jsonElement.getString(NetworkUtility.MEDIA_TITLE);
				GalleryContent content = new GalleryContent(contentID, resourceUrl, description, type);
				contents.add(content);
			}		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return contents;
	}
	
	public static Event getDetailEvent(String jsonResponse){
		Event event = new Event();
		String startDate;
		String endDate;
		String description;
		String article;
		String place;
		String coverUrl;
		try {
			JSONObject jsonRp = new JSONObject(jsonResponse);
			JSONObject jsonQuestion = jsonRp.getJSONObject(NetworkUtility.QUESTION);
			JSONObject jsonTable = jsonQuestion.getJSONObject(NetworkUtility.TABLE);
			event.setEventID(jsonTable.getLong(NetworkUtility.ID));
			event.setEventName(jsonTable.getString(NetworkUtility.EVENTNAME));
			event.setStartDate(jsonTable.getString(NetworkUtility.STARTDATE));
			event.setCoverUrl(jsonTable.getString(NetworkUtility.THUMBURL));
			event.setEndDate(jsonTable.getString(NetworkUtility.ENDDATE));
			event.setPlace(jsonTable.getString(NetworkUtility.PLACE));
			event.setArticle(jsonTable.getString(NetworkUtility.ARTICLE));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return event;
	}
}
